//引入express
var express = require('express')
var multer = require('multer')
var fs = require('fs/promises')
var path = require('path')
var upload = multer()

//引入路由
var apps = express.Router()
var UUID = require('uuid')
let db = require('../utils/db')

// 图片上传接口
apps.post('/uploadImg', upload.single('file'), async function(req, res, next) {
  const {file} = req
  const data = {
    url: ''
  }
  try {
    const {originalname, buffer} = file
    const filePath = `./uploads/${originalname}`
    await fs.writeFile(filePath, buffer)
    data.url = 'http://localhost:8888/' + originalname
  } catch (error) {
    console.log(error)
    res.json('上传失败')
  }
  res.json(data)
})

// 获取所有商品
apps.get('/getAllGoods', function(req, res) {

  let sql = `SELECT * FROM goods`
  db.query(sql, (err, data) => {
    if (data) {
      res.json({
        code: 200,
        msg: '商品查询成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '商品查询失败'
      })
    }
  })
})

// 模糊查询商品
apps.get('/getSearch', function(req, res) {
  let {goodsName, goodsState} = req.query
  let name = decodeURI(goodsName)
  name = `%${name}%`
  let sql = `SELECT * FROM goods WHERE NAME LIKE ? AND shelf= ?`
  db.query(sql, [name, goodsState], (err, data) => {
    if (data) {
      res.json({
        code: 200,
        msg: '商品查询成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '商品查询失败'
      })
    }
  })
})

// 增加商品
apps.post('/addAllGoods', function(req, res) {
  let {name, cost_price, supply_price, sell_price, information, goods_classify_id, store_id, stoke, create_time, deleted, sall_num, image_url} = req.body
  let sql = `INSERT INTO goods (name,cost_price,supply_price,sell_price,information,goods_classify_id,store_id,stoke,create_time,deleted,sall_num,image_url)VALUES (?,?,?,?,?,?,?,?,?,?,?,?) `
  db.query(sql, [name, cost_price, supply_price, sell_price, information, goods_classify_id, store_id, stoke, create_time, deleted, sall_num, image_url], (err, data) => {
    console.log(err)
    if (data) {
      res.json({
        code: 200,
        msg: '增加商品成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '增加商品失败'
      })
    }
  })
})

// 编辑商品
apps.post('/editAllGoods', function(req, res) {
  let {name, cost_price, supply_price, sell_price, information, goods_classify_id, store_id, stoke, create_time, deleted, sall_num, image_url} = req.body
  let sql = `INSERT INTO goods (name,cost_price,supply_price,sell_price,information,goods_classify_id,store_id,stoke,create_time,deleted,sall_num,image_url)VALUES (?,?,?,?,?,?,?,?,?,?,?,?) `
  db.query(sql, [name, cost_price, supply_price, sell_price, information, goods_classify_id, store_id, stoke, create_time, deleted, sall_num, image_url], (err, data) => {
    console.log(err)
    if (data) {
      res.json({
        code: 200,
        msg: '修改商品成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '修改商品失败'
      })
    }
  })
})

// 编辑商品上下架状态(单个)
apps.post('/editShelf', function(req, res) {
  let {shelf, id} = req.body
  console.log(req.body)
  let sql = `UPDATE goods SET shelf = ? WHERE id = ?`
  db.query(sql, [shelf, id], (err, data) => {
    console.log(err)
    if (data) {
      res.json({
        code: 200,
        msg: '修改成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '修改失败'
      })
    }
  })
})

// 编辑商品上下架状态(批量)
apps.post('/editShelfes', function(req, res) {
  let {ids} = req.body
  let sql = `UPDATE goods SET shelf = 0 WHERE id in (${ids})`

  db.query(sql, (err, data) => {
    if (data) {
      res.json({
        code: 200,
        msg: '批量修改成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '批量修改失败'
      })
    }
  })





})

// 删除商品
apps.post('/delGoods', function(req, res) {
  let {id} = req.body
  let sql = `DELETE FROM goods WHERE id = ?`
  db.query(sql, [id], (err, data) => {
    if (data) {
      res.json({
        code: 200,
        msg: '删除成功',
      })
    } else {
      res.json({
        code: 500,
        msg: '删除失败'
      })
    }
  })
})

// 是否推荐接口
apps.post('/switchGoods', function(req, res) {
  let {id, switch1} = req.body
  let sql = `UPDATE goods SET switch = ? WHERE id = ?`
  db.query(sql, [switch1, id], (err, data) => {
    if (data) {
      res.json({
        code: 200,
        msg: '修改成功',
      })
    } else {
      res.json({
        code: 500,
        msg: '修改失败'
      })
    }
  })
})

// 增加商品数量
apps.post('/addGoodsNum', function(req, res) {
  let {num, id} = req.body
  let sql = `UPDATE goods SET num = ? WHERE id = ? `
  db.query(sql, [num, id], (err, data) => {
    if (data) {
      res.json({
        code: 200,
        msg: '增加数量编辑成功',
      })
    } else {
      res.json({
        code: 500,
        msg: '增加数量编辑失败'
      })
    }
  })
})

// 减少商品数量
apps.post('/reduceGoodsNum', function(req, res) {
  let {num, id} = req.body
  let sql = `UPDATE goods SET num = ? WHERE id = ? `
  db.query(sql, [num, id], (err, data) => {
    if (data) {
      res.json({
        code: 200,
        msg: '减少数量编辑成功',
      })
    } else {
      res.json({
        code: 500,
        msg: '减少数量编辑失败'
      })
    }
  })
})

// 获取商品类别
apps.get('/getClassify', function(req, res) {

  let sql1 = `SELECT * FROM goods_classify`
  let sql2 = `SELECT * FROM goods_classify_child`

  db.query(sql1, (err1, data1) => {
    console.log(sql1)
    if (data1) {
      db.query(sql2, (err2, data2) => {
        if (data2) {
          res.json({
            code: 200,
            msg: '查询成功',
            data1,
            data2
          })
        }
      })
    } else {
      res.json({
        code: 500,
        msg: '查询失败'
      })
    }
  })
})

// 通过分类名称查询
apps.get('/searchClassify', function(req, res) {
  const {name} = req.query
  let sql1 = `SELECT * FROM goods_classify WHERE NAME = ?`
  let sql2 = `SELECT * FROM goods_classify_child`

  db.query(sql1, [name], (err1, data1) => {
    if (data1) {
      db.query(sql2, (err2, data2) => {
        if (data2) {
          res.json({
            code: 200,
            msg: '查询成功',
            data1,
            data2
          })
        }
      })
    } else {
      res.json({
        code: 500,
        msg: '查询失败'
      })
    }
  })
})

// 增加一级类别
apps.post('/addCategory1', function(req, res) {
  let {name1, image_url1, state} = req.body
  let sql = `INSERT INTO goods_classify(NAME, image_url, state) VALUES(?,?,?)`
  db.query(sql, [name1, image_url1, state], (err, data) => {
    console.log(data)
    if (data) {
      res.json({
        code: 200,
        msg: '添加成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '添加失败'
      })
    }
  })
})

// 增加二级类别
apps.post('/addCategory2', function(req, res) {
  let {name2, image_url2, id, state} = req.body
  let sql = `INSERT INTO goods_classify_child(NAME, goods_classify_id, image_url, state) VALUES(?,?,?,?)`
  db.query(sql, [name2, id, image_url2, state], (err, data) => {
    console.log(err)
    if (data) {
      res.json({
        code: 200,
        msg: '添加成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '添加失败'
      })
    }
  })
})

// 编辑一级分类上下架状态
apps.post('/editState1', function(req, res) {
  let {state, id} = req.body
  console.log(req.body)
  let sql = `UPDATE goods_classify SET state = ? WHERE id = ?`
  db.query(sql, [state, id], (err, data) => {
    console.log(data)
    if (data) {
      res.json({
        code: 200,
        msg: '修改成功',
      })
    } else {
      res.json({
        code: 500,
        msg: '修改失败'
      })
    }
  })
})

// 编辑二级分类上下架状态
apps.post('/editState2', function(req, res) {
  let {state, id} = req.body
  console.log(req.body)
  let sql = `UPDATE goods_classify_child SET state = ? WHERE id = ?`
  db.query(sql, [state, id], (err, data) => {
    console.log(data)
    if (data) {
      res.json({
        code: 200,
        msg: '修改成功',
      })
    } else {
      res.json({
        code: 500,
        msg: '修改失败'
      })
    }
  })
})

// 编辑一级分类
apps.post('/editCategory1', function(req, res) {
  let {name1, image_url1, id, state} = req.body
  console.log(req.body)
  let sql = `UPDATE goods_classify SET name = ?,image_url=?,state=? WHERE id = ?`
  db.query(sql, [name1, image_url1, state, id], (err, data) => {
    console.log(data)
    if (data) {
      res.json({
        code: 200,
        msg: '修改成功',
      })
    } else {
      res.json({
        code: 500,
        msg: '修改失败'
      })
    }
  })
})

// 编辑二级分类
apps.post('/editCategory2', function(req, res) {
  let {name2, image_url2, id, state} = req.body
  console.log(req.body)
  let sql = `UPDATE goods_classify_child SET name = ?,image_url=?,state=? WHERE id = ?`
  db.query(sql, [name2, image_url2, state, id], (err, data) => {
    console.log(data)
    if (data) {
      res.json({
        code: 200,
        msg: '修改成功',
      })
    } else {
      res.json({
        code: 500,
        msg: '修改失败'
      })
    }
  })
})

// 删除一级分类
apps.post('/delClassify1', function(req, res) {
  let {id} = req.body
  let sql = `DELETE FROM goods_classify WHERE id = ?`
  db.query(sql, [id], (err, data) => {
    console.log(data)
    if (data) {
      res.json({
        code: 200,
        msg: '删除成功',
      })
    } else {
      res.json({
        code: 500,
        msg: '删除失败'
      })
    }
  })
})

// 删除二级分类
apps.post('/delClassify2', function(req, res) {
  let {id} = req.body
  let sql = `DELETE FROM goods_classify_child WHERE id = ?`
  db.query(sql, [id], (err, data) => {
    console.log(data)
    if (data) {
      res.json({
        code: 200,
        msg: '删除成功',
      })
    } else {
      res.json({
        code: 500,
        msg: '删除失败'
      })
    }
  })
})

//获取所有店铺
apps.get('/getAllStoreInf', function(req, res) {
  let sql = 'select * from store'
  db.query(sql, function(err, data) {
    if (data) {
      res.json({
        code: 200,
        msg: '查询成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '查询失败'
      })
    }
  })
})

// 获取所有商店对应的订单
apps.get('/getAllorderInf', function(req, res) {
  let sql = 'SELECT store_id, SUM(actual_payment)AS payment FROM orderr GROUP BY store_id'
  db.query(sql, function(err, data) {
    if (data) {
      res.json({
        code: 200,
        msg: '查询成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '查询失败'
      })
    }
  })
})

// b端登录接口
apps.post('/bLogin', function(req, res) {
  const {user_name, password} = req.body
  let sql = 'SELECT * FROM `user` WHERE user_name=? AND PASSWORD=?'
  db.query(sql, [user_name, password], function(err, data) {
    console.log(err)
    if (data) {
      res.json({
        code: 200,
        msg: '登录成功',
        data
      })
    } else {
      res.json({
        code: 500,
        msg: '登录失败'
      })
    }
  })
})


module.exports = apps